//
//  StatusCellTopView.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/11.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit

//MARK: - 顶部视图
class StatusCellTopView: UIView {
    
    ///单条微博 视图模型
    var viewModel: StatusViewModel? {
        didSet {

            iconView.sd_setImageWithURL(viewModel?.userIconViewURL, placeholderImage: viewModel?.userDefaultIcon)

            nameLabel.text = viewModel?.status.user?.screen_name

            memberIconView.image = viewModel?.userMemberIcon
            vipIconView.image = viewModel?.userVipIcon
            timeLabel.text = "刚刚"//viewModel?.status?.created_at
            sourceLabel.text = viewModel?.status.source?.href()?.text
//            QL1(sourceLabel.text)

        }
    }

    /// 构造方法
    ///
    /// - returns: UIView
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(imageName: "avatar_default_big")
    /// 姓名
    private lazy var nameLabel: UILabel = UILabel(title: "hello", textSize: 14)
    /// 成员图标
    private lazy var memberIconView: UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    /// 会员图标
    private lazy var vipIconView: UIImageView = UIImageView(imageName: "avatar_vip")
    /// 时间标签
    private lazy var timeLabel: UILabel = UILabel(title: "刚刚", color: UIColor.orangeColor(), textSize: 11)
    /// 来源标签
    private lazy var sourceLabel: UILabel = UILabel(title: "来源", textSize: 11)
}

// MARK: - 设置 UI
extension StatusCellTopView {
    
    private func setupUI() {
        
        //顶部分隔视图
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGrayColor()
        addSubview(sepView)
        
        //添加子控件       
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)

        
        //设置自动布局
         sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
/// 头像图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.width.equalTo(StatusCellIconWidth)
            make.height.equalTo(StatusCellIconWidth)
        }
        
/// 昵称标签
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
/// 成员图标
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
/// 会员图标
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
/// 时间标签
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
/// 来源标签
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        
    }
}












